<template>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="ftp主键" prop="ftpId" v-if="dataForm.ftpId">
        <el-input v-model="dataForm.ftpId" placeholder="ftp主键" disabled></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="ftpAddress">
        <el-input v-model="dataForm.ftpAddress" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="ftp名称" prop="ftpName">
        <el-input v-model="dataForm.ftpName" placeholder="ftp名称"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="ftpUserName">
        <el-input v-model="dataForm.ftpUserName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="ftpPwd">
        <el-input v-model="dataForm.ftpPwd" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="端口" prop="ftpPort">
        <el-input v-model="dataForm.ftpPort" placeholder="端口"></el-input>
    </el-form-item>
    <el-form-item label="类型" prop="ftpType">
        <el-radio v-model="dataForm.ftpType" label="0">仅下载</el-radio>
        <el-radio v-model="dataForm.ftpType" label="1">仅上传</el-radio>
        <el-radio v-model="dataForm.ftpType" label="2">上传和下载</el-radio>
    </el-form-item>
    <el-form-item label="网络环境" prop="ftpNetwork">
        <el-radio v-model="dataForm.ftpNetwork" :label="true">内网（内网访问）</el-radio>
        <el-radio v-model="dataForm.ftpNetwork" :label="false">外网（DMZ区访问）</el-radio>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy" v-if="dataForm.id">
        <el-input v-model="dataForm.createBy" placeholder="创建者" disabled></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
        <el-input v-model="dataForm.createTime" placeholder="创建时间" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy" v-if="dataForm.id">
        <el-input v-model="dataForm.updateBy" placeholder="更新者" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime" v-if="dataForm.id">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间" disabled></el-input>
    </el-form-item>
    </el-form>
</template>

<script>
    import {getFtpInfoById, addFtpInfo, putObj, checkFtpStatus} from '@/api/gather/configftp'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          ftpId: '',
          ftpAddress: '',
          ftpUserName: '',
          ftpName: '',
          ftpPwd: '',
          ftpPort: '',
          ftpType: '',
          ftpNetwork: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: ''
        },
        dataRule: {
          ftpAddress: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          ftpUserName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
           ftpName: [
            { required: true, message: 'ftp名称不能为空', trigger: 'blur' }
          ],
          ftpPwd: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          ftpPort: [
            { required: true, message: '端口不能为空', trigger: 'blur' }
          ],
          ftpType: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          ftpNetwork: [
            { required: true, message: '网络环境不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      initByEntity (ftpInfo) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if(ftpInfo.id)this.dataForm.id = ftpInfo.id;
          if(ftpInfo.ftpAddress)this.dataForm.ftpAddress = ftpInfo.ftpAddress;
          if(ftpInfo.ftpUserName)this.dataForm.ftpUserName = ftpInfo.ftpUserName;
          if(ftpInfo.ftpName)this.dataForm.ftpName = ftpInfo.ftpName;
          if(ftpInfo.ftpPwd)this.dataForm.ftpPwd = ftpInfo.ftpPwd;
          if(ftpInfo.ftpPort)this.dataForm.ftpPort = ftpInfo.ftpPort;
          if(ftpInfo.ftpType)this.dataForm.ftpType = ftpInfo.ftpType;
          if(ftpInfo.ftpNetwork)this.dataForm.ftpNetwork = ftpInfo.ftpNetwork;
        })
      },
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (id) {
            getFtpInfoById(id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      checkFtpStatus () {
        checkFtpStatus(this.dataForm).then(data => {
            if (data.data.code === 0) {
              this.$notify.success(data.data.data)
            } else {
              this.$notify.error(data.data.data)
            }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.ftpId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.disvisible()
                    this.$emit('refreshData')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addFtpInfo(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.disvisible()
                    this.$emit('refreshData',data.data.data.ftpId)
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      disvisible () {
        this.$emit('disvisible')
      },
      //重置表单
      closeDialog() {
          this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
